package com.put.mybills.database;

import java.util.ArrayList;
import java.util.List;
import com.put.mybills.model.Bill;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class BillDataSource {
	
	
	  private static final String TAG = "BillDataSource";
		
	  private SQLiteDatabase database;
	  private DatabaseHelper dbHelper;	  

	  private String[] allColumns = { 	DatabaseHelper.BILL_ID,
			  							DatabaseHelper.BILL_FACTUREID,
			  							DatabaseHelper.BILL_NAME,
			  							DatabaseHelper.BILL_TERM,
			  							DatabaseHelper.BILL_PRICE,
			  							DatabaseHelper.BILL_DESCRIPTION,
			  							DatabaseHelper.BILL_PICTURE_PATH,
			  							DatabaseHelper.BILL_IMAGE_DRIVE_ID,
			  							DatabaseHelper.BILL_IFPAID};
	  

	  public BillDataSource(Context context) {
	    dbHelper = new DatabaseHelper(context);
	  }
	
	  public void open() throws SQLException {
	    database = dbHelper.getWritableDatabase();
	  }
	
	  public void close() {
	    dbHelper.close();
	  }
	  
	  
	  public Bill createBill(Bill bill) {  
		  	
		  	Log.d(TAG, "Create bill function");
			ContentValues values = new ContentValues();
			values.put(DatabaseHelper.BILL_NAME, bill.getName());
			values.put(DatabaseHelper.BILL_FACTUREID, bill.getFactureId());		
			values.put(DatabaseHelper.BILL_DESCRIPTION, bill.getDescription());		
			values.put(DatabaseHelper.BILL_TERM, bill.getDate());		
			values.put(DatabaseHelper.BILL_PRICE, bill.getPrice());
			values.put(DatabaseHelper.BILL_PICTURE_PATH, bill.getPicturePath());
			values.put(DatabaseHelper.BILL_IFPAID, bill.getIfPaid());
			
			if (!bill.getDriveIDPicture().equals(""))
				values.put(DatabaseHelper.BILL_IMAGE_DRIVE_ID, bill.getDriveIDPicture());
			
			long insertId = database.insert(DatabaseHelper.BILL_TABLE_NAME, null, values);
			
			Cursor cursor = database.query(DatabaseHelper.BILL_TABLE_NAME,
			    allColumns, DatabaseHelper.BILL_ID + " = " + insertId, null,
			    null, null, null);
			
			cursor.moveToFirst();			
			Bill newBill = cursorToBill(cursor);
			cursor.close();
			return newBill;		
			   
	  }
	  
	  
	  public void updateBill(Bill bill) {  
		  	
		  	Log.d(TAG, "Update bill function");
			ContentValues values = new ContentValues();
			values.put(DatabaseHelper.BILL_NAME, bill.getName());
			values.put(DatabaseHelper.BILL_FACTUREID, bill.getFactureId());		
			values.put(DatabaseHelper.BILL_DESCRIPTION, bill.getDescription());		
			values.put(DatabaseHelper.BILL_TERM, bill.getDate());		
			values.put(DatabaseHelper.BILL_PRICE, bill.getPrice());
			values.put(DatabaseHelper.BILL_PICTURE_PATH, bill.getPicturePath());
			
			Log.d(TAG,  "IFPAID " + bill.getIfPaid());
			values.put(DatabaseHelper.BILL_IFPAID, bill.getIfPaid());
			if (bill.getDriveIDPicture() != null && !bill.getDriveIDPicture().equals(""))
				values.put(DatabaseHelper.BILL_IMAGE_DRIVE_ID, bill.getDriveIDPicture());
			
			String strFilter = DatabaseHelper.BILL_ID + "=" + bill.getId();
			database.update(DatabaseHelper.BILL_TABLE_NAME, values, strFilter,null);
	  }
	  
	  
	  
	  public void deleteBill(Bill bill) {
		    long id = bill.getId();
		    //System.out.println("Comment deleted with id: " + id);
		    database.delete(DatabaseHelper.BILL_TABLE_NAME, DatabaseHelper.BILL_ID + " = " + id, null);
	  }
	  
	  public void deleteAllFacture() {		  
		    //System.out.println("Bill deleted");
		    database.delete(DatabaseHelper.BILL_TABLE_NAME, null, null);
	  }
	  
	  public List<Bill> getAllBills() {
		    List<Bill> bills = new ArrayList<Bill>();

		    Cursor cursor = database.query(DatabaseHelper.BILL_TABLE_NAME, allColumns, null, null, null, null, null);

		    cursor.moveToFirst();
		    while (!cursor.isAfterLast()) {
		    	Bill bill = cursorToBill(cursor);
		    	bills.add(bill);
		    	cursor.moveToNext();
		    }		  
		    cursor.close();
		    return bills;
	  }
	  
	  public Bill getBill(String billName)
	  {		  
		  Bill bill = null;
		  Cursor cursor = database.query(DatabaseHelper.BILL_TABLE_NAME, allColumns, null, null, null, null, null);

		  cursor.moveToFirst();
		  while (!cursor.isAfterLast()) {
			  Bill b = cursorToBill(cursor);
		    	
			  if (b.getName().equals(billName))
				  bill = b;
		    	
		      cursor.moveToNext();
		    }
		    cursor.close();
		    return bill;	  
	  }
	  

	  public List<Bill> getAllBills(Integer factureID) {
		  
		  
		    List<Bill> bills = new ArrayList<Bill>();

		    Cursor cursor = database.query(DatabaseHelper.BILL_TABLE_NAME, allColumns, null, null, null, null, null);

		    cursor.moveToFirst();
		    while (!cursor.isAfterLast()) {
		    	Bill bill = cursorToBill(cursor);		    	
		    	if (bill.getFactureId().equals(factureID))
		    		bills.add(bill);
		    	
		      cursor.moveToNext();
		    }
		    // make sure to close the cursor
		    cursor.close();
		    return bills;
	  }
	  
	  
 
	  private Bill cursorToBill(Cursor cursor) {
		  Bill bill = new Bill();
		  bill.setId(cursor.getInt(0));
		  bill.setFactureId(cursor.getInt(1));
		  bill.setName(cursor.getString(2));
		  bill.setDate(cursor.getString(3));
		  bill.setPrice(cursor.getDouble(4));
		  bill.setDescription(cursor.getString(5));
		  bill.setPicturePath(cursor.getString(6));
		  bill.setDriveIDPicture(cursor.getString(7));	
		  bill.setIfPaid(cursor.getInt(8));
		  
		  bill.setTmpDate(bill.getDate());
		  		  
		  return bill;
	  }

}
